//
//  XZCustomCamer.m
//  封装选择图片和 调用系统相机
//
//  Created by Henry on 2017/9/20.
//  Copyright © 2017年 kmwl. All rights reserved.
//

#import "XZCustomCamer.h"
#import "PrivacyPermission.h"

static XZCustomCamer *_instace = nil;

@interface XZCustomCamer ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

/* type **/
@property (nonatomic, strong) UIImagePickerController *imagePickerVC;

@end

@implementation XZCustomCamer

+ (instancetype)shareCustom {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instace = [[self alloc] init];
    });
    return _instace;
}

- (UIImagePickerController *)imagePickerVC {
    if (!_imagePickerVC) {
        _imagePickerVC = [[UIImagePickerController alloc] init];
        _imagePickerVC.delegate = self;
        _imagePickerVC.allowsEditing = YES;
    }
    return _imagePickerVC;
}

- (void)pushPhotoAlbumcholoes:(XZCustomCamerType)type setPhotoBlock:(PhotoBlock)block {
    
    
    [[PrivacyPermission sharedInstance] accessPrivacyPermissionWithType:(PrivacyPermissionTypeCamera) completion:^(BOOL response, PrivacyPermissionAuthorizationStatus status) {
        if (response == NO) {
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                [LEEAlert alert].config.LeeTitle(@"提醒").LeeContent(@"您可以在”设置“中为此应用程序打开相机权限").LeeAddAction(^(LEEAction *action) {
                    [action setTitle:@"设置"];//60    70    118
                    action.backgroundColor = kColorWithRGB(60, 70, 118);
                    action.titleColor = [UIColor whiteColor];
                    action.clickBlock = ^{
                        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                        if([[UIApplication sharedApplication] canOpenURL:url]) {
                            [[UIApplication sharedApplication] openURL:url];
                        }
                    };
                }).LeeAddAction(^(LEEAction *action) {
                    [action setTitle:@"取消"];
                    [action setTitleColor:[UIColor whiteColor]];
                    [action setTitleColor:kColorWithRGB(60, 70, 118)];
                }).LeeShadowOpacity(3).LeeCornerRadius(5).LeeShadowOpacity(0.4).LeeShow();
            }];
        }
        return;
    }];
    
    if (type == 0) {
        self.imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    } else {
        self.imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:_imagePickerVC animated:YES completion:nil];
    
    _photoBlock = [block copy];
}

// 选择图片成功调用此方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    // dismiss UIImagePickerController
    [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:^{
        
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImage *image = info[UIImagePickerControllerOriginalImage];
            if (_photoBlock) {
                _photoBlock(image);
            }
        } else {
            UIImage *image = info[UIImagePickerControllerOriginalImage];
            if (_photoBlock) {
                _photoBlock(image);
            }
        }
    }];
}

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    if ([navigationController isKindOfClass:[UIImagePickerController class]])
    {
        // 取消不透明
        viewController.navigationController.navigationBar.translucent = NO;
        
        // 按钮颜色
        viewController.navigationController.navigationBar.tintColor = [UIColor blackColor];
        //标题颜色
        [viewController.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        
        // 导航栏颜色
        viewController.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
//        viewController.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

@end
